package com.jessechanrui.template.fop;

import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import java.io.File;
import java.io.FileOutputStream;

public class FopPdfGenerator {
    public static void main(String[] args) throws Exception {
        // 1. 初始化FOP工厂
        FopFactory fopFactory = FopFactory.newInstance(new File("E:\\SourceCode\\Gitee_SourceCode\\template-collection\\pdf-template\\pdfbox-template\\src\\main\\java\\com\\jessechanrui\\template\\fop\\fop.xconf"));
        // 2. 创建PDF输出流
        FileOutputStream out = new FileOutputStream("./employee-info.pdf");
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
        // 3. 关联XSL-FO模板和数据源，执行转换
        Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource("E:\\SourceCode\\Gitee_SourceCode\\template-collection\\pdf-template\\pdfbox-template\\src\\main\\java\\com\\jessechanrui\\template\\fop\\user-template.fo"));
        transformer.transform(new StreamSource("E:\\SourceCode\\Gitee_SourceCode\\template-collection\\pdf-template\\pdfbox-template\\src\\main\\java\\com\\jessechanrui\\template\\fop\\user.xml"), new SAXResult(fop.getDefaultHandler()));
        // 4. 关闭流
        out.close();
    }
}